package com.teaRestaurant.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import com.teaRestaurant.common.result.Result;
import com.teaRestaurant.domain.DTO.CategoryPageQueryDTO;
import com.teaRestaurant.domain.DTO.DishPageQueryDTO;
import com.teaRestaurant.domain.VO.DishPageQueryVO;
import com.teaRestaurant.domain.entity.Category;
import com.teaRestaurant.domain.entity.Dishes;
import com.teaRestaurant.service.DishService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品相关接口")
@CrossOrigin
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 获取菜品列表
     * 筛选条件：名称，分类，售卖状态
     */
    @GetMapping
    public Result list (DishPageQueryDTO dishPageQueryDTO) {
        PageInfo<DishPageQueryVO> dishesIPage = dishService.list(dishPageQueryDTO);
        return Result.success(dishesIPage);
    }

    /**
     * 通过分类id获取起售菜品
     * @param id
     * @return
     */
    @GetMapping("/listByCId/{id}")
    public Result listByCId (@PathVariable("id") Long id) {
        List<Dishes> dishesList = dishService.listByCId(id);
        return Result.success(dishesList);
    }

    /**
     * 查询菜品通过id
     */
    @GetMapping("/{id}")
    public Result getById (@PathVariable("id") Long id) {
        DishPageQueryVO  dishes =  dishService.getById(id);
        return Result.success(dishes);
    }

    /**
     * 新增菜品
     */
    @PostMapping
    public Result insert (@RequestBody Dishes dishes){
        dishService.insert(dishes);
        return Result.success();
    }

    /**
     * 修改菜品
     */
    @PutMapping
    public Result update (@RequestBody Dishes dishes) {
        dishService.update(dishes);
        return Result.success();
    }

    /**
     * 删除菜品
     */
    @DeleteMapping
    public Result delete (@RequestParam String ids) {
        List<Long> ids2 = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());
        dishService.delete(ids2);
        return Result.success();
    }


    /**
     * 修改菜品状态状态
     */
    @PutMapping("/updateStatus/{id}")
    public Result updateStatus (@PathVariable("id") Long id) {
        dishService.updateStatus(id);
        return Result.success();
    }

    /**
     * 查看起售菜品数量
     */
    @GetMapping("/getDishSellCount")
    public Result getDishSellCount (){
        Map map = dishService.getDishSellCount();
        return Result.success(map);
    }

}
